<template>
  <el-dialog :title="form.id? '编辑部门' : '新增部门'" :visible="showadd" width="40%" @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="请输入部门编码" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请输入部门负责人">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" placeholder="请输入部门介绍" />
      </el-form-item>
    </el-form>
    <template #footer>

      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirmadd">确 定</el-button>
    </template>

  </el-dialog>

</template>

<script>
import { getdepartments, getEmployeeSimple, adddepartments, queryDepartmentsMeassage, changeDepartments } from '@/api/departments'

export default {
  props: {
    showadd: {
      type: Boolean,
      required: true
    },
    currentid: {
      type: String,
      default: ''
    }
  },
  data() {
    const validateName = async(rule, value, callback) => {
      const res = await getdepartments()
      if (this.form.id) {
        if (this.oldname === value) {
          callback()
        } else {
          const is = res.depts.filter(item => item.pid === this.form.pid).some(item => item.name === value)
          is ? callback(new Error('同级部门名字重复')) : callback()
        }
      }

      const is = res.depts.filter(item => item.pid === this.currentid).some(item => item.name === value)
      is ? callback(new Error('子级部门名字重复')) : callback()
    }
    const validateCode = async(rule, value, callback) => {
      const res = await getdepartments()
      if (this.form.id) {
        if (this.oldcode === value) {
          callback()
        } else {
          res.depts.some(item => item.code === value) ? callback(new Error('部门id重复')) : callback()
        }
      }
      res.depts.some(item => item.id === value) ? callback(new Error('部门id重复')) : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''

      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-50个字符', trigger: 'blur' }
        ]
      },
      options: [],
      oldname: '',
      oldcode: ''
    }
  },
  created() {
    this.getEmployeeSimples()
  },
  methods: {
    // 点击取消,叉号关闭添加界面
    close() {
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.form.resetFields()
      this.$emit('offshowadd')
    },
    // 获取所有员工,渲染到选项
    async getEmployeeSimples() {
      const res = await getEmployeeSimple()
      this.options = res
    },
    // 确认添加子部门或编辑部门,并更新渲染
    confirmadd() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        // 区分是编辑还是新增
        if (this.form.id) {
          await changeDepartments({
            ...this.form
          })
        } else {
          await adddepartments({
            ...this.form,
            pid: this.currentid
          })
        }
        this.close()
        this.$message.success('操作成功')
        this.$emit('updata')
      })
    },

    //
    async backshow(id) {
      const res = await queryDepartmentsMeassage(id)
      this.oldname = res.name
      this.oldcode = res.code
      console.log(res)
      this.form = res
    }
  }
}
</script>
